﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseFrame.Tool.CefSharp.FileResource
{
    public class TestJsonFilter : IResponseFilter
    {
        public List<byte> DataAll = new List<byte>();


        public FilterStatus Filter(System.IO.Stream dataIn, out long dataInRead, System.IO.Stream dataOut, out long dataOutWritten)
        {
            try
            {
                if (dataIn == null || dataIn.Length == 0)
                {
                    dataInRead = 0;
                    dataOutWritten = 0;

                    return FilterStatus.Done;
                }

                //dataInRead = dataIn.Length;
                //dataOutWritten = Math.Min(dataInRead, dataOut.Length);
                //if (dataIn.Length > dataOut.Length)
                //{
                //    dataOut = new MemoryStream(Convert.ToInt32(dataIn.Length));
                //}

                if (dataIn.Length > dataOut.Length)
                {
                    var data = new byte[dataOut.Length];
                    dataIn.Seek(0, SeekOrigin.Begin);
                    dataIn.Read(data, 0, data.Length);
                    dataOut.Write(data, 0, data.Length);

                    dataInRead = dataOut.Length;
                    dataOutWritten = dataOut.Length;

                    DataAll.AddRange(data);
                }
                else
                {
                    dataInRead = dataIn.Length;
                    dataOutWritten = Math.Min(dataInRead, dataOut.Length);

                    dataIn.CopyTo(dataOut);
                    dataIn.Seek(0, SeekOrigin.Begin);
                    byte[] bs = new byte[dataIn.Length];
                    dataIn.Read(bs, 0, bs.Length);
                    DataAll.AddRange(bs);

                    dataInRead = dataIn.Length;
                    dataOutWritten = dataIn.Length;
                }

                //if (dataIn.Length > dataOut.Length)
                //{
                //    var data = new byte[dataOut.Length];
                //    dataIn.Seek(0, SeekOrigin.Begin);
                //    dataIn.Read(data, 0, data.Length);
                //    dataOut.Write(data, 0, data.Length);
                //    dataIn.Position = 0;
                //    dataIn.CopyTo(dataOut);
                //    dataInRead = dataOut.Length;
                //    dataOutWritten = dataOut.Length;
                //}
                //else {
                //    dataIn.CopyTo(dataOut);
                //    dataIn.Seek(0, SeekOrigin.Begin);
                //    byte[] bs = new byte[dataIn.Length];
                //    dataIn.Read(bs, 0, bs.Length);
                //    DataAll.AddRange(bs);
                //    dataInRead = dataIn.Length;
                //    dataOutWritten = dataIn.Length;
                //}


                return FilterStatus.NeedMoreData;
            }
            catch (Exception ex)
            {
                dataInRead = dataIn.Length;
                dataOutWritten = dataIn.Length;

                return FilterStatus.Done;
            }
        }

        public bool InitFilter()
        {
            return true;
        }

        public void Dispose()
        {

        }
    }
    public class FilterManager
    {
        private static Dictionary<string, IResponseFilter> dataList = new Dictionary<string, IResponseFilter>();

        public static IResponseFilter CreateFilter(string guid)
        {
            lock (dataList)
            {
                var filter = new TestJsonFilter();
                dataList.Add(guid, filter);

                return filter;
            }
        }

        public static IResponseFilter GetFileter(string guid)
        {
            try
            {
                lock (dataList)
                {
                    var result = dataList[guid];
                    dataList.Remove(guid);
                    return result;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }

        }
    }
}
